// utils/auth.js
export function getJwtPayload() {
    const token = localStorage.getItem('token');
    if (!token) return null;

    const parts = token.split('.');
    if (parts.length < 2) return null;

    try {
        // base64url -> bytes -> utf8
        const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
        const binary = atob(base64);
        const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
        const json = new TextDecoder('utf-8').decode(bytes);
        return JSON.parse(json);
    } catch (e) {
        console.error('解析 JWT 失败:', e);
        return null;
    }
}
